package com.jastar.android.test.abstracts;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.jastar.android.test.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.stream.IntStream;

public abstract class AbstractExamActivity extends AppCompatActivity implements View.OnClickListener {

    protected Random random = new Random(System.currentTimeMillis());

    private int[] pics = {R.drawable.pic_spiderman_1, R.drawable.pic_spiderman_2, R.drawable.pic_spiderman_3, R.drawable.pic_spiderman_4};
    private int powerMax = 100;             //最大能量
    private int powerToLevelup = 10;        //每级能量

    private TextView tv_title;
    private TextView tv_question;
    private TextView tv_right_count;
    private TextView tv_wrong_count;
    private ImageView iv_main_pic;
    private TextView tv_main_power;

    protected List<View> button_answers;

    private Object answer;
    private int index;
    private int rightCount;
    private int wrongCount;
    private int level;
    private int start_main_width;       //初始图片宽度
    private int start_main_height;      //初始图片高度

    private AlertDialog rightDialog;
    private AlertDialog wrongDialog;

    protected int getLevel() {
        return level;
    }

    protected void beforeRender() {
    }

    protected void afterRender() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getActivityId());

        tv_title = findViewById(R.id.tv_title);
        tv_question = findViewById(R.id.tv_question);
        tv_right_count = findViewById(R.id.tv_right_count);
        tv_wrong_count = findViewById(R.id.tv_wrong_count);
        iv_main_pic = findViewById(R.id.iv_main_pic);
        tv_main_power = findViewById(R.id.tv_main_power);
        button_answers = new ArrayList<>();
        button_answers.add(findViewById(R.id.button_answer_1));
        button_answers.add(findViewById(R.id.button_answer_2));
        button_answers.add(findViewById(R.id.button_answer_3));
        button_answers.add(findViewById(R.id.button_answer_4));
        random = new Random(System.currentTimeMillis());
        index = 0;
        rightCount = 0;
        wrongCount = 0;
        start_main_width = iv_main_pic.getLayoutParams().width;
        start_main_height = iv_main_pic.getLayoutParams().height;

        beforeRender();
        refreshMain();
        initDialog();
        nextQuestion();
        afterRender();

        button_answers.forEach(button -> button.setOnClickListener(this));
    }

    private void refreshMain() {
        int power = Math.min(Math.max(rightCount - wrongCount, 0), powerMax);
        level = (power / powerToLevelup) + 1;
        if (level <= 0) level = 1;
        iv_main_pic.setImageResource(pics[Math.min(pics.length - 1, level - 1)]);
        refreshWidthAndHeight(iv_main_pic, power);
        tv_main_power.setText(String.valueOf(power));
    }

    private void initDialog() {
        rightDialog = new AlertDialog.Builder(this, R.style.themes_math_alert_right).setIcon(R.drawable.pic_expression_smile)
                .setTitle("答对了!").setPositiveButton("确定", (dialog, which) -> nextQuestion()).create();
        wrongDialog = new AlertDialog.Builder(this, R.style.themes_math_alert_wrong).setIcon(R.drawable.pic_expression_sad)
                .setTitle("答错了!").setPositiveButton("确定", (dialog, which) -> nextQuestion()).create();
    }

    private void nextQuestion() {
        QuestionItem item = getNextQuestionItem(button_answers.size());
        answer = item.rightAnswer;
        index++;

        tv_title.setText(format("第%d题：", index));
        tv_question.setText(item.question);
        tv_right_count.setText(format("正确： %d", rightCount));
        tv_wrong_count.setText(format("错误： %d", wrongCount));
        refreshMain();

        IntStream.range(0, button_answers.size())
                .forEach(i -> setButtonByAnswer(button_answers.get(i), item.answers.get(i)));
    }

    private void refreshWidthAndHeight(View view, int power) {
        int baseLayoutNum = powerToLevelup * 3 / 2;
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = start_main_width * (baseLayoutNum + power) / baseLayoutNum;
        layoutParams.height = start_main_height * (baseLayoutNum + power) / baseLayoutNum;
        view.setLayoutParams(layoutParams);
    }

    @Override
    public void onClick(View v) {
        if (isRightAnswer(v, answer)) {
            rightDialog.show();
            rightCount++;
        } else {
            wrongDialog.show();
            wrongCount++;
        }
    }

    protected abstract int getActivityId();

    protected abstract QuestionItem getNextQuestionItem(int answerCount);

    protected abstract void setButtonByAnswer(View view, Object o);

    protected abstract boolean isRightAnswer(View v, Object answer);

    protected String format(String format, Object... args) {
        return String.format(Locale.getDefault(), format, args);
    }

    public static class QuestionItem {
        public String question;
        public List<Object> answers = new ArrayList<>();
        public Object rightAnswer;
    }

}